from rest_framework import serializers
from task.models import Task

class TeacherSerializer(serializers.ModelSerializer):
    # 显式定义别名字段
    m_name = serializers.CharField(source='module.name', read_only=True)
    l_name = serializers.CharField(source='language.name', read_only=True)

    # 如果需要显示用户名称而不是ID，也可以添加这个
    user_names = serializers.SerializerMethodField()

    def get_user_names(self, obj):
        return list(obj.users.values_list('username', flat=True))

    class Meta:
        model = Task
        fields = [
            'id', 'title', 'detail', 'completed', 'task_time',
            'create_time', 'delete_time', 'is_delete', 'update_time',
            'language', 'module', 'users',  # 保留原始字段
            'm_name', 'l_name',  # 添加别名字段
            'user_names'  # 如果需要显示用户名
        ]